import { ref } from "vue";

export function useProjectNumber() {
  // 获取当前日期（YYYYMMDD格式）
  const getCurrentDate = () => {
    const today = new Date();
    return [
      today.getFullYear(),
      String(today.getMonth() + 1).padStart(2, "0"),
      String(today.getDate()).padStart(2, "0"),
    ].join("");
  };

  // 生成项目编号
  const generateProjectNumber = () => {
    const dateStr = getCurrentDate();
    const storageKey = `projectCounter_${dateStr}`;

    // 从 localStorage 获取或初始化计数器
    let counter = parseInt(localStorage.getItem(storageKey)) || 0;
    counter++;

    // 更新存储
    localStorage.setItem(storageKey, counter.toString());

    // 返回格式化的项目编号
    return `${dateStr}${String(counter).padStart(2, "0")}`;
  };

  return {
    generateProjectNumber,
  };
}
